using System;
using System.Collections.Generic;
using System.Net;
using SpoonCore2.Client;
using SpoonCore2.Message;
using System.IO;
using SpoonCore2.Message.Filter;
using SpoonCore2.Message.Header;

namespace SpoonCore2.Service
{
	public abstract class AbstractService : IService
	{
		public event OnSendDataHandler OnSendData;
		public event OnSendDataHandlerInfo OnSendDataInfo;
		public event OnSendDataHandlerEndpoint OnSendDataEndpoint;

		private readonly MessageHandler _handler = new MessageHandler();
		private ServiceManager _manager;
		private ServiceStatus _status;

		public void Send(SpoonIdentifier id, IMessage message)
		{
			if (OnSendData != null) OnSendData(this, id, message);
		}

		public void Send(ClientInfo info, IMessage message)
		{
			if (OnSendDataInfo != null) OnSendDataInfo(this, info, message);
		}

		public void Send(IPEndPoint endpoint, IMessage message)
		{
			if (OnSendDataEndpoint != null) OnSendDataEndpoint(this, endpoint, message);
		}

		public abstract SpoonIdentifier Id { get;}

		public MessageHandler Handler
		{
			get { return _handler; }
		}

		public ServiceManager Manager
		{
			get { return _manager; }
		}

		public abstract void Dispose();
		public abstract void Init();
		public abstract void Start();
		public abstract void Stop();
		public ServiceStatus Status { get { return _status; } }

		internal void SetStatus(ServiceStatus status)
		{
			_status = status;
		}

		public void Receive(ClientInfo info, BinaryReader reader, IList<IMessageOption> options, IList<IMessageFilterInfo> filters)
		{
			Handler.Receive(this, info, reader, options, filters);
		}

		public void SetManager(ServiceManager manager)
		{
			_manager = manager;
		}
	}
}